package com.nit.lab.controller;

import com.nit.lab.dto.DtoStore;
import com.nit.lab.dto.utils.DtoAjaxMsg;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.dto.utils.DtoDataTableReturn;
import com.nit.lab.dto.DtoSysFile;
import com.nit.lab.model.SysFileEntity;
import com.nit.lab.service.FileService;
import com.nit.lab.utils.SystemConstants;
import org.jboss.logging.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author be_yond
 * @date 2017/10/25
 */
@Controller
@Scope("prototype")
@RequestMapping("/fileController")
public class FileController {
    private static Logger log = Logger.getLogger(UserController.class.getClass());

    @Resource
    private FileService fileService;

    @ResponseBody
    @RequestMapping("/getAllFile")
    public DtoDataTableReturn getAllFile(DtoDataTable dtoDataTable, Integer sessionId, Integer actorId) {
        DtoDataTableReturn jsonList = null;
        Integer pre;
        try {
            if (sessionId == null) {
                pre = -1;
            } else {
                pre = sessionId;
            }
            Integer count = fileService.getFile4DataTableCount(dtoDataTable.getSearch(), pre, actorId);
            List<DtoSysFile> dtoSysFiles = fileService.getFileDataTable(dtoDataTable, pre, actorId);
            jsonList = new DtoDataTableReturn(dtoDataTable.getDraw(), count, count, dtoSysFiles);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return jsonList;
    }

    @ResponseBody
    @RequestMapping("/uploadFile")
    public Integer uploadFile(String fileUrl, Integer perNameId, String fileName) {
        SysFileEntity sysFileEntity = new SysFileEntity();
        try {
            sysFileEntity.setFileName(fileName);
            sysFileEntity.setFileUrl(fileUrl);
            sysFileEntity.setUserId(perNameId);
            sysFileEntity.setBrowseCount(perNameId);
            fileService.uploadFile(sysFileEntity);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return 1;
    }

    @ResponseBody
    @RequestMapping("/updataFileInfo")
    public DtoAjaxMsg updataFileInfo(SysFileEntity sysFileEntity) {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result = fileService.updataFileInfo(sysFileEntity);
            if (result >= SystemConstants.Update_Success) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_SUCCESS);
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
        }
        return dtoAjaxMsg;
    }

    @RequestMapping(value = "/deleteFile")
    @ResponseBody
    public DtoAjaxMsg deleteFile(Integer fileId) throws Exception {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            String result = fileService.deleteFile(fileId);
            if (result != null && "Success".equals(result)) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Success);
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
            }
        } catch (Exception e) {
            e.printStackTrace();
            dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
        }
        return dtoAjaxMsg;
    }


    @ResponseBody
    @RequestMapping("/getAllFileForUser")
    public DtoDataTableReturn getAllFileForUser(DtoDataTable dtoDataTable, Integer sessionId) {
        DtoDataTableReturn jsonList = null;
        try {
            Integer count = fileService.getFile4DataTableForUserCount(dtoDataTable.getSearch(), sessionId);
            List<DtoSysFile> dtoSysFiles = fileService.getFileDataTableForUser(dtoDataTable, sessionId);
            jsonList = new DtoDataTableReturn(dtoDataTable.getDraw(), count, count, dtoSysFiles);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return jsonList;
    }

    @ResponseBody
    @RequestMapping("/getAllStore")
    public DtoAjaxMsg getAllStore() {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            List<DtoStore> dtoStores = fileService.getAllStore();
            if (dtoStores.size() > 0) {
                dtoAjaxMsg.setAjaxInfo("Success");
                dtoAjaxMsg.setAjaxObject(dtoStores);
            } else {
                dtoAjaxMsg.setAjaxInfo("Faile");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return dtoAjaxMsg;
    }
}
